using Avalonia.BiliBili.Libs.Models.View;
using Avalonia.Controls;
using Avalonia.Extensions.Controls;
using Avalonia.Extensions.Event;
using Bilibili.Common;
using Bilibili.Common.Log;
using Bilibili.Model.Comment;
using Bilibili.Model.Common;
using Bilibili.WebClient.Api;
using DynamicData;
using System;
using System.Collections.Generic;

namespace Avalonia.BiliBili.Dialogs
{
    public partial class EmojiDialog : WindowBase
    {
        public event CallbackStringClick OnClick;
        public EmojiDialog()
        {
            InitializeComponent();
        }
        protected override void AfterInitializeComponent()
        {
            Height = 60;
            Width = 100;
        }
        public void InitEmoji(EmoteMode mode)
        {
            RunOnUiThread(() =>
            {
                var array = new List<TabControlItem<EmoteItem>>();
                EmoteClient.Instance.EmotePackages.Foreach(packs =>
                {
                    if (packs.Item1 == mode)
                    {
                        var data = packs.Item2;
                        var tabItem = new TabControlItem<EmoteItem>(data.Text);
                        tabItem.Nodes.AddRange(data.Emote);
                        array.Add(tabItem);
                    }
                });
                emojiList.ItemsSource = array;
            });
        }
        private void OnItemClick(object? sender, ViewRoutedEventArgs e)
        {
            try
            {
                if (e.ClickItem is GridViewItem item && item.Content is EmoteItem emote)
                {
                    OnClick?.Invoke(this, emote.Text);
                    Hide();
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("OnItemClick", ex);
            }
        }
        protected override void OnClosing(WindowClosingEventArgs e)
        {
            try
            {
                Hide();
                e.Cancel = true;
            }
            catch { }
            base.OnClosing(e);
        }
    }
}